package com.lagou.demo.controller;

import com.lagou.demo.service.IDemoService;
import com.lagou.edu.mvcframework.annotations.LagouAutowired;
import com.lagou.edu.mvcframework.annotations.LagouController;
import com.lagou.edu.mvcframework.annotations.LagouRequestMapping;
import com.lagou.edu.mvcframework.annotations.MySecurity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@MySecurity({"zhangwei", "ziqiao", "haitang", "meijia"})
@LagouController
@LagouRequestMapping("/demo")
public class DemoController {

    @LagouAutowired
    private IDemoService demoService;

    /**
     * URL: /demo/query?name=lisi
     *
     * @param request
     * @param response
     * @param name
     * @return
     */
    @LagouRequestMapping("/query")
    public String query(HttpServletRequest request, HttpServletResponse response, String name) {
        return demoService.get(name);
    }

    @LagouRequestMapping("/apartment")
    public void apartment(HttpServletRequest request, HttpServletResponse response, String username) throws IOException {
        hello(response, "apartment", username);
    }

    @MySecurity({"zhangwei", "ziqiao", "haitang"})
    @LagouRequestMapping("/man")
    public void man(HttpServletRequest request, HttpServletResponse response, String username) throws IOException {
        hello(response, "man", username);
    }

    @MySecurity({"meijia"})
    @LagouRequestMapping("/woman")
    public void woman(HttpServletRequest request, HttpServletResponse response, String username) throws IOException {
        hello(response, "woman", username);
    }

    @MySecurity({"ziqiao", "meijia"})
    @LagouRequestMapping("/married")
    public void married(HttpServletRequest request, HttpServletResponse response, String username) throws IOException {
        hello(response, "married", username);
    }

    private void hello(HttpServletResponse response, String method, String username) throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().println(method + ": Hello " + username);
    }
}
